package com.practice.blog.common.advice;

import com.practice.blog.common.exception.BlogException;
import com.practice.blog.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-30
 * Time: 12:06
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result handle(Exception e) {
        log.error("发生异常, e: {}",e);
        return Result.fail("发生异常" + e.getMessage());
    }

    @ExceptionHandler(BlogException.class)
    public Result blogExceptionHandle(Exception e) {
        log.error("发生错误, e: {}",e);
        return Result.fail(e.getMessage());
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoResourceFoundException.class)
    public Result handle(NoResourceFoundException e) {
        log.error("文件不存在, e: {}",e.getResourcePath());
        return Result.fail("文件不存在" + e.getResourcePath());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({HandlerMethodValidationException.class, MethodArgumentNotValidException.class})
    public Result argHandle(Exception e) {
        log.error("参数校验失败, e: {}",e.getMessage());
        return Result.fail("参数不合法");
    }

}
